<?php

class PeriodoliquidacionController extends GxController {


	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'PeriodoLiquidacion'),
		));
	}

	public function actionCreate() {
		$model = new PeriodoLiquidacion;


		if (isset($_POST['PeriodoLiquidacion'])) {
			$model->setAttributes($_POST['PeriodoLiquidacion']);

			if ($model->save()) {
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->PeriodoId));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionCreateLiquidacion() {
		
		if (isset($_POST['Anio'])) {
			$anio = $_POST['Anio'];
			$periodoinicio = $_POST['PeriodoInicio'];
			$periodofin = $_POST['PeriodoFin'];
				
			$sql = 'CALL liquidar_periodos(:anio, :periodoinicio, :periodofin)';
			$command = Yii::app()->db->createCommand($sql);
			$command->bindParam(":anio", $anio, PDO::PARAM_INT);
			$command->bindParam(":periodoinicio", $periodoinicio, PDO::PARAM_INT);
			$command->bindParam(":periodofin", $periodofin, PDO::PARAM_INT);
				
			$command->execute();
			
			// the following line raise the error
// 			$errorInfo = Yii::app()->db->createCommand("select @error_info as result;")->queryScalar();

			$this->redirect(array('deudasocio/admin'));
		}

		$this->render('createliquidacion', array( ));
	}

	public function actionCreateLiquidacionFrom($id) {
		
		if (isset($_POST['Anio'])) {
			$anio = $_POST['Anio'];
			$periodoinicio = $_POST['PeriodoInicio'];
			$periodofin = $_POST['PeriodoFin'];
						
			$sql = 'CALL liquidar_periodos_socio(:socioid, :anio, :periodoinicio, :periodofin)';
			$command = Yii::app()->db->createCommand($sql);
			$command->bindParam(":socioid", $id, PDO::PARAM_INT);
			$command->bindParam(":anio", $anio, PDO::PARAM_INT);
			$command->bindParam(":periodoinicio", $periodoinicio, PDO::PARAM_INT);
			$command->bindParam(":periodofin", $periodofin, PDO::PARAM_INT);
							
			$command->execute();
			
			// the following line raise the error
// 			$errorInfo = Yii::app()->db->createCommand("select @error_info as result;")->queryScalar();

			$this->redirect(array('socio/view', 'id' => $id));
		}

		$this->render('createliquidacion', array('id' => $id));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'PeriodoLiquidacion');


		if (isset($_POST['PeriodoLiquidacion'])) {
			$model->setAttributes($_POST['PeriodoLiquidacion']);

			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->PeriodoId));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, 'PeriodoLiquidacion')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('PeriodoLiquidacion');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new PeriodoLiquidacion('search');
		$model->unsetAttributes();

		if (isset($_GET['PeriodoLiquidacion']))
			$model->setAttributes($_GET['PeriodoLiquidacion']);

		$this->render('admin', array(
			'model' => $model,
		));
	}

	public function actionSelectPeriodoFin() {
		if (isset($_POST['PeriodoInicio'])) {
			$id = $_POST['PeriodoInicio'];
			$lista = PeriodoLiquidacion::model()->findAll('PeriodoId >= :id', array(':id' => $id));
			echo CHtml::tag('option', array('value' => ''), 'Seleccione un Periodo ... ', true);
			foreach($lista as $periodo) {
				$valor = $periodo['PeriodoId'];
				$descripcion = $periodo['Descripcion'];
				echo CHtml::tag('option', array('value' => $valor), CHtml::encode($descripcion), true);
			}
		}
	}
	
}